package com.lepu.app.fun.index.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.app.fun.my.MyArticleInfoActivity;
import com.lepu.app.fun.my.bean.BeanMyArticle;
import com.lepu.pasm.R;

public class IndexVideoAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanMyArticle> mArrayList = null;

	public IndexVideoAdapter(Context context,ArrayList<BeanMyArticle> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}
	
	public IndexVideoAdapter(Context context,ArrayList<BeanMyArticle> arrayList, boolean isCollect) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size()/2+1;
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.index_video_list_item, null);

			holder = new ViewHolder();

			holder.itemLayout1 = (LinearLayout) convertView.findViewById(R.id.itemLayout1);
			holder.iconImageView1 = (ImageView) convertView.findViewById(R.id.iconImageView1);
			holder.pointImageView1 = (ImageView) convertView.findViewById(R.id.pointImageView1);
			holder.titleTextView1 = (TextView) convertView.findViewById(R.id.titleTextView1);
			holder.collectTextView1 = (TextView) convertView.findViewById(R.id.collectTextView1);
			holder.playTextView1 = (TextView) convertView.findViewById(R.id.playTimesTextView1);
			
			holder.itemLayout2 = (LinearLayout) convertView.findViewById(R.id.itemLayout2);
			holder.iconImageView2 = (ImageView) convertView.findViewById(R.id.iconImageView2);
			holder.pointImageView2 = (ImageView) convertView.findViewById(R.id.pointImageView2);
			holder.titleTextView2 = (TextView) convertView.findViewById(R.id.titleTextView2);
			holder.collectTextView2 = (TextView) convertView.findViewById(R.id.collectTextView2);
			holder.playTextView2 = (TextView) convertView.findViewById(R.id.playTimesTextView2);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if(mArrayList.size() > position*2){
			holder.itemLayout1.setVisibility(View.VISIBLE);
			
			final BeanMyArticle item1 = mArrayList.get(position*2);
			UniversalImageLoadTool.disPlay(item1.Cover, holder.iconImageView1,R.drawable.default_icon_small);
			holder.titleTextView1.setText(item1.ArticleTitle);
			holder.collectTextView1.setText("收藏  " + item1.CollectCount);
			holder.playTextView1.setText("· 播放  " + item1.ViewCount);

			//未读加点
			if(BeanIndexNew.isExist(item1.ArticleID)){
				holder.pointImageView1.setVisibility(View.GONE);
			}else{
				holder.pointImageView1.setVisibility(View.VISIBLE);
			}
			
			holder.itemLayout1.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							BaseFragmentActivity activity = (BaseFragmentActivity) mContext;
							Intent intent = new Intent(mContext,MyArticleInfoActivity.class);
							intent.putExtra("ArticleID",item1.ArticleID);
							intent.putExtra("position",position*2);
							activity.startActivity(intent, true);
							
							BeanIndexNew.insertTargetId(item1.ArticleID);
							holder.pointImageView1.setVisibility(View.GONE);
						}
					});
		}else{
			holder.itemLayout1.setVisibility(View.GONE);
		}
		
		
		if(mArrayList.size() > position*2+1){
			holder.itemLayout2.setVisibility(View.VISIBLE);
			
			final BeanMyArticle item2 = mArrayList.get(position*2+1);
			UniversalImageLoadTool.disPlay(item2.Cover, holder.iconImageView2,R.drawable.default_icon_small);
			holder.titleTextView2.setText(item2.ArticleTitle);
			holder.collectTextView2.setText("收藏  " + item2.CollectCount + "");
			holder.playTextView2.setText("· 播放  " + item2.ViewCount + "");

			//未读加点
			if(BeanIndexNew.isExist(item2.ArticleID)){
				holder.pointImageView2.setVisibility(View.GONE);
			}else{
				holder.pointImageView2.setVisibility(View.VISIBLE);
			}
			
			holder.itemLayout2.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							BaseFragmentActivity activity = (BaseFragmentActivity) mContext;
							Intent intent = new Intent(mContext,MyArticleInfoActivity.class);
							intent.putExtra("ArticleID",item2.ArticleID);
							intent.putExtra("position",position*2+1);
							activity.startActivity(intent, true);
							
							BeanIndexNew.insertTargetId(item2.ArticleID);
							holder.pointImageView2.setVisibility(View.GONE);
						}
					});
		}else{
			holder.itemLayout2.setVisibility(View.INVISIBLE);
		}
	

		return convertView;
	}

	private static class ViewHolder {
		LinearLayout itemLayout1;
		ImageView iconImageView1;
		ImageView pointImageView1;
		TextView titleTextView1;
		TextView collectTextView1;
		TextView playTextView1;
		
		LinearLayout itemLayout2;
		ImageView iconImageView2;
		ImageView pointImageView2;
		TextView titleTextView2;
		TextView collectTextView2;
		TextView playTextView2;
	}
}
